package com.system.controller;

import com.system.pojo.UserLogin;
import com.system.server.UserService;
import exception.LoginError;
import exception.OldPasswordError;
import exception.passwordDiffError;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年04月09日22:38
 * @Description: <描述>
 */
@Controller
public class UserTypeController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public String login(String username, String password, Model model, HttpSession session) {
        //Shiro实现登录
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        Subject subject = SecurityUtils.getSubject();

        //如果获取不到用户名就是登录失败，但登录失败的话，会直接抛出异常
        try {
            subject.login(token);
            UserLogin userLogin = new UserLogin();
            userLogin.setUsername(username);

            session.setAttribute("userLogin",userLogin);
            if (subject.hasRole("admin")) {
                return "redirect:/admin/findAll";
            } else if (subject.hasRole("teacher")) {
                return "redirect:/teacher/findAll";
            } else if (subject.hasRole("student")) {
                return "redirect:/student/findAll";
            }
        } catch (UnknownAccountException e) {
            model.addAttribute("loginError","用户名不存在");
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("loginError","密码不正确");
        } catch (AuthenticationException e) {
            model.addAttribute("loginError","登录失败");
        }
        return "/login";
    }
    @RequestMapping("/passwordRestPage")
    public String passwordRestPage() {

        return "/admin/passwordRestPage";
    }
    @RequestMapping("/passwordRest")
    public String passwordRest(String oldPassword,String newPassword,String aginaNewPassword,HttpSession session,Model model) {
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");
        try {
            System.out.println(oldPassword+":"+newPassword+":"+userLogin);
            userLogin = userService.findUserLoginByName(userLogin.getUsername());
            System.out.println("UserTypeController.passwordRest:"+userLogin);

            userService.updatePassword(oldPassword,newPassword,aginaNewPassword, userLogin.getUserID());
            return "/login";
        } catch (OldPasswordError e) {
            model.addAttribute("PasswordError",e.getMessage());
        } catch (exception.passwordDiffError e) {
            model.addAttribute("PasswordError",e.getMessage());
        }
        return "/admin/passwordRestPage";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "/login";
    }

}
